package com.bys.system.service.impl;

import com.bys.system.model.SysDeptModel;
import com.bys.system.mapper.SysDeptMapper;
import com.bys.system.service.ISysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bys.system.vo.SysDeptVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * PackageName:  com.bys.system.service.impl
 * ClassName:    SysDeptServiceImpl
 * Description:  组织机构 服务实现类
 * Author:       bys
 * Date:         2023-03-31
 **/
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDeptModel> implements ISysDeptService {
    @Resource
    private SysDeptMapper deptMapper;

    @Override
    public List<SysDeptVo> getAllDeptTree(Long id) {
        List<SysDeptVo> deptVoList = deptMapper.selectAllDeptTree(id);
        convertDeptTree(deptVoList);
        return deptVoList;
    }

    private void convertDeptTree(List<SysDeptVo> deptList) {
        if (deptList != null) {
            deptList.forEach(dept -> {
                if (dept != null && dept.getId() != null) {
                    List<SysDeptVo> child = deptMapper.selectAllDeptTree(dept.getId());
                    dept.setChild(child);
                    if(dept.getChild() == null || dept.getChild().size()< 1){
                        dept.setChild(null);
                    }
                    convertDeptTree(child);
                }
            });
        }
    }
}
